var controller = {
	/**
	 * 初始化
	 */
	init : function() {
		var _this = this;
		_this.bindClick();
	},
	/**
	 * 绑定事件
	 */
	bindEvent : function() {
		var _this = this;
		_this.bindClick()
	},
	bindClick : function() {
		$("#addResourceBtn").bind("click", function() {
			// 资源属性
			var resource_name = $("#resourceName").val();
			var resource_desc = $("#resourceDesc").val();
			var resource_type = $("#resourceType").val();
			var resource_String = $("#resourceUrl").val();
			var enabled = $("input[name='enabled']:checked").val();
			var issys = $("input[name='issys']:checked").val();
			if (enabled == 'on') {
				enabled = 1;
			} else {
				enabled = 0;
			}
			if (issys == 'on') {
				issys = 1;
			} else {
				issys = 0;
			}
			var parent_id = $('#parentResource option:selected').val();
			if (parent_id == "unselect") {
				parent_id = "0";
			}
			var resource = {
				resource_name : resource_name,
				resource_desc : resource_desc,
				resource_type : resource_type,
				resource_String : resource_String,
				enabled : enabled,
				issys : issys,
				parent_id : parent_id
			}
			var resourceString = JSON.stringify(resource);
			ajax.addResource("/resource/add", resourceString);
		});

		$("#addDialogBtn").bind("click", function() {
			ajax.loadParent();
			$("#editResourceBtn").remove();
			$("#addDialog").modal('show');
		});
		
		$(".remove_resource").bind("click", function() {
			// 资源属性
			var resource_id = $(this).val();
			ajax.deleteResource("/resource/delete", resource_id);
		});
		
		$(".edit_resource").bind("click", function() {
			// 资源属性
			ajax.loadParent();
			var resource_id = $(this).val();
			$("#addResourceBtn").remove();
			var resource = ajax.load(resource_id);
			$("#resourceId").val(resource.resource_id);
			$("#resourceName").val(resource.resource_name);
			$("#resourceDesc").val(resource.resource_desc);
			$("#resourceType").val(resource.resource_type);
			$("#resourceUrl").val(resource.resource_String);
			if(resource.enabled == 1) {
				$("#enabled").parent().attr("aria-checked","true");
				$("#enabled").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			if(resource.issys == 1) {
				$("#issys").parent().attr("aria-checked","true");
				$("#issys").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			if(resource.parent_id == '0') {
				$("#parentResource").val('unselect');
			}else {
				$("#parentResource").val(resource.parent_id);
			}
			//###################
			$("#addDialog").modal('show');
		});
		
		$("#editResourceBtn").bind("click", function() {
			// 资源属性
			var resource_id = $("#resourceId").val();
			var resource_name = $("#resourceName").val();
			var resource_desc = $("#resourceDesc").val();
			var resource_type = $("#resourceType").val();
			var resource_String = $("#resourceUrl").val();
			var enabled = $("input[name='enabled']:checked").val();
			var issys = $("input[name='issys']:checked").val();
			if (enabled == 'on') {
				enabled = 1;
			} else {
				enabled = 0;
			}
			if (issys == 'on') {
				issys = 1;
			} else {
				issys = 0;
			}
			
			var parent_id = $('#parentResource option:selected').val();
			if (parent_id == "unselect") {
				parent_id = "0";
			}

			var resource = {
				resource_id : resource_id,
				resource_name : resource_name,
				resource_desc : resource_desc,
				resource_type : resource_type,
				resource_String : resource_String,
				enabled : enabled,
				issys : issys,
				parent_id : parent_id
			}
			var resourceString = JSON.stringify(resource);
			ajax.editResource("/resource/edit", resourceString);
		});
	}
};
var ajax = {
	addResource : function(url, data) {
		$.ajax({
			url : url,
			type : "POST",
			async : false,
			dataType : "json",
			data : data,
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				$("#addDialog").modal('hide')
				if (rs.success) {
					App.myDialog("添加菜单成功", "提示");
				} else {
					App.myDialog("添加菜单失败：" + rs.result, "提示");
				}
			}
		});
	},
	loadParent : function() {
		$.ajax({
			url : "/firstMenus",
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					$("#parentResource").html("");
					$("#parentResource").append(
							"<option value='unselect'>未选择</option>");
					for (var i = 0; i < rs.result.length; i++) {
						$("#parentResource").append(
								"<option value='" + rs.result[i].resource_id
										+ "'>" + rs.result[i].resource_desc
										+ "</option>");
					}
				} else {
					App.myDialog("获取一级菜单失败：" + rs.result, "提示");
				}
			}
		});
	},
	deleteResource : function(url, data) {
		$.ajax({
			url : url + "?resourceId=" + data,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					App.myDialog("删除菜单成功", "提示");
				} else {
					App.myDialog("删除菜单失败：" + rs.result, "提示");
				}
			}
		});
	},
	load : function(data) {
		var result;
		$.ajax({
			url : "/resource?resourceId=" + data,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					result = rs.result;
				} else {
					App.myDialog("获取菜单详情失败：" + rs.result, "提示");
				}
			}
		});
		
		return result;
	},
	editResource : function(url, data) {
		$.ajax({
			url : url,
			type : "POST",
			async : false,
			dataType : "json",
			data : data,
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				$("#addDialog").modal('hide')
				if (rs.success) {
					App.myDialog("修改菜单成功", "提示");
				} else {
					App.myDialog("修改菜单失败：" + rs.result, "提示");
				}
			}
		});
	}
};
$(function() {
	controller.init();
});